package connection
{
	// NE SERT QUE POUR UNE VISUALISATION AVEC PPV
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import mx.core.Application;
	import mx.controls.Alert;
	import mx.core.UIComponent;
	import composants.BitmapLoader;
	
	public class Porte extends UIComponent
	{
		public var x1:int;
		public var y1:int;
		public var idPorte:String;
		public var color:uint;
		public var ouverture:BitmapLoader = new BitmapLoader("icons/porte.png");
		public var tabColors:Array = new Array();
		public var iscolored:Boolean;
		
		public function Porte(x1:int, y1:int,idP:String,col:uint)
		{	
			this.x1 = x1;
			this.y1 = y1;
			this.idPorte = idP;
			this.color = col;
			this.addEventListener(MouseEvent.MOUSE_DOWN, clickOuverture);
			if (this.color == 0xffff00)
				iscolored = false;
			else 
				iscolored = true;
			TweenMax.to(this, 1, {colorMatrixFilter:{colorize:this.color, amount:1, threshold:0}});
		}
		
		public function paint(ix1:int,iy1:int,ix2:int,iy2:int):void {
			ouverture.rotation = (-Math.atan(((ix1-ix2)/(iy1-iy2)))/Math.PI*180+90); //prOuverture;
			ouverture.x = x1;
			ouverture.y = y1;
			ouverture.visible  = true;
			addChild(ouverture);
		}
		
		public function clickOuverture(e:MouseEvent):void {
			if (Application.application.verificationColor(Application.application.colorSelected) < 2
				&& Application.application.verificationIdSalleColor((Salle)(this.parent).idSalle,Application.application.colorSelected) == false)
			{
				this.setColor(Application.application.colorSelected);
				TweenMax.to(this, 1, {colorMatrixFilter:{colorize:this.getColor(), amount:1, threshold:0}});
			}
			else
			{
				if (Application.application.verificationIdSalleColor((Salle)(this.parent).idSalle,Application.application.colorSelected) == true)
					Alert.show("Vous ne pouvez pas connecter deux portes de la meme salle");
				else if (Application.application.verificationColor(Application.application.colorSelected) >= 2)
					Alert.show("Vous avez deja utilise cette couleur pour deux portes");
			}
		}
		
		public function getBmpPorte():BitmapLoader {
			return ouverture;
		}
		
		public function setColor(c:uint):void {
			this.color = c;
			iscolored = true;
		}
		
		public function getColor():uint
		{
			return this.color;
		}
	}
}
